/**
 * @author Amamatthew
 * @class DragonApp.view.pub.form.FileUpload
 * @extends extend: 'Ext.form.Panel',
 *
 *  实际用例:
 *
 */
Ext.define('DragonApp.view.resource.FileUpload', {
    extend: 'Ext.form.Panel',
    alias: 'widget.taguploadform',
    config: {
        destUrl: undefined
    },
    bodyPadding: 10,
    frame: true,
    items: [{
        xtype: 'filefield',
        name: 'file',
        itemId: 'filefield',
        fieldLabel: '标签',
        labelWidth: 70,
        msgTarget: 'side',
        allowBlank: false,
        anchor: '100%',
        buttonText: '选择文件...'
    }, {
        xtype: 'container',
        items: [{
            xtype: 'image',
            alt: '图像预览',
            src: '',
            width: 184,
            height: 90,
            margin: '10 0 0 0',
            itemId: 'previewfield'
        }]
    }, {
        xtype: 'hiddenfield',
        fieldLabel: '图像标签',
        itemId: 'tag',
        name: 'tag'
    }, {
        xtype: 'displayfield',
        labelWidth: 70,
        fieldLabel: '图像标签',
        name: 'tag_preview'
    }],
    buttons: [{
        text: '上传',
        itemId: 'uploadBtn',
        url: undefined,
        handler: function(button, e) {
            var form = this.up('form');
            var basicform = this.up('form').getForm();
            var formValue = basicform.getValues();

            if (basicform.isValid()) {

                //标签输入提示
                Ext.Msg.prompt("提示", "请输入图像标签：", function(btn, text) {

                    if ("ok" == btn) {
                        var tagField = button.up('form').queryById('tag');
                        var txt = text.trim();

                        if (Ext.isEmpty(txt)) {

                            DragonApp.util.Msg.showToast('请输入图像标签:');

                        } else {

                            tagField.setValue(txt);

                            //上传文件提交
                            basicform.submit({
                                url: button.up('form').destUrl,
                                waitMsg: '上传之中...',
                                success: function(form, action) {

                                    //成功提示
                                    DragonApp.util.Msg.showToast(action.result.msg);

                                    var data = action.result.data;

                                    // 更新预览
                                    var filename = data.filename;
                                    var container = Ext.ComponentQuery.query('taguploadform>container')[0];
                                    var previewfield = container.queryById('previewfield');

                                    previewfield.setSrc(DragonApp.ux.config.Config.getTagUrl(filename));

                                    //图像标签
                                    var tagField = Ext.ComponentQuery.query('displayfield[fieldLabel="图像标签"]')[0];
                                    tagField.setValue('[ ' + data.tag + ' ]');

                                    // 增加记录
                                    var store = Ext.data.StoreManager.lookup('tagStore');
                                    store.remove(store.queryRecords('filename', data.filename)); //删除同名记录

                                    store.add(Ext.create('DragonApp.model.Image', {
                                        ext: data.ext,
                                        tag: data.tag,
                                        path: data.path,
                                        width: data.width,
                                        height: data.height,
                                        filename: data.filename,
                                        filesize: data.filesize,
                                        lastmodtime: data.lastmodtime
                                    }));
                                    return;
                                },
                                failure: function(form, action) {

                                    switch (action.failureType) {
                                        case Ext.form.action.Action.CLIENT_INVALID:
                                            Ext.Msg.alert('提示', 'Form fields may not be submitted with invalid values');
                                            break;
                                        case Ext.form.action.Action.CONNECT_FAILURE:
                                            Ext.Msg.alert('提示', 'Ajax communication failed');
                                            break;
                                        case Ext.form.action.Action.SERVER_INVALID:
                                            Ext.Msg.alert('提示', action.result.msg);
                                    }
                                }
                            });
                        }
                    }
                }, null, true, null, {
                    autoCapitalize: true,
                    placeHolder: '请输入标签名称...'
                });
            }
        }
    }],
    initComponent: function() {
        me = this;
        me.callParent(arguments);

        var filefield = me.queryById('filefield');
        filefield.setFieldLabel(this.fieldLabel);
    }
});